<template>
  <view class="content">
    <view class="btn">
      <button class="new" type="primary" @click="handleAdd">+添加供应商</button>
    </view>
    <view class="content-box">
      <view
        class="item-box-ng"
        v-for="(item, index) in lists"
        :url="`/packageB/pages/common/adress-editing/index?id=${item.id}`"
      >
        <view class="item-box" @click="handleDetail(item.id)">
          <text class="cph">{{ item.customerName }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { getList } from '@/api/supplier';
import { getDictList } from '@/api/login';

export default {
  data() {
    return {
      proName: '',
      coalTypeList: [],
      lists: []
    };
  },

  async onShow() {
    this.lists = [];
    this.getSupplierList();
  },

  onPullDownRefresh() {
    this.lists = [];
    this.getSupplierList();
  },

  onReachBottom() {
    this.getSupplierList();
  },

  methods: {
    handleAdd() {
      uni.setStorageSync('title', '添加');
      uni.navigateTo({
        url: '/packageB/pages/common/supplier-editing/index'
      });
    },

    handleDetail(id) {
      uni.redirectTo({
        url: `/packageB/pages/supplier/details?id=${id}`
      });
    },

    linkToRecords(id) {
      uni.navigateTo({
        url: `/packageB/pages/product/records?id=${id}`
      });
    },

    confirmChange(data) {
      this.lists = [];
      this.getSupplierList();
    },

    async getDictList() {
      const res = await getDictList();
      if (res.code === 0) {
        this.coalTypeList = (res?.data || []).filter(
          (e) => e.dictType === 'coal_type'
        );
      }
    },

    //获取列表
    async getSupplierList() {
      try {
        const res = await getList();
        if (res.code == 0) {
          this.lists = res.data || [];
        }
        wx.stopPullDownRefresh();
      } catch (e) {
        return false;
      }
    },

    exchangeCoalTypeLabel(result) {
      return result.map((e) => {
        e.productType = this.coalTypeList.find(
          (m) => m.value === e.productType.toString()
        )?.label;
        return e;
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.phxx-list {
  padding: 20rpx;
  .item + .item {
    margin-top: 20px;
  }
}
</style>
